package com.itboye.lottery.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.bylibrary.util.TimeUtils;
import com.itboye.lottery.R;
import com.itboye.lottery.bean.MessageBean;
import com.itboye.lottery.utils.OnItemClickListener;

import java.util.List;

/**
 * Created by  HONGDA on 2018/6/19.
 */
public class MessageListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity context;
    public List<MessageBean> mData;
    private LayoutInflater inflater;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public MessageListAdapter(Activity context, List<MessageBean> mData) {
        this.context = context;
        this.mData = mData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_message, parent, false);
        MessageViewHolder viewHolder = new MessageViewHolder(view);
        return viewHolder;
    }

//    public void refresh(List<ResHotPeopleInfo.HotPeopleOrderBean> datas) {
//        if (mData != null) {
//            mData.clear();
//            addData(datas);
//        }
//    }
//
//    public void addData(List<ResHotPeopleInfo.HotPeopleOrderBean> datas) {
//        mData.addAll(datas);
//        notifyDataSetChanged();
//    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MessageViewHolder viewHolder = (MessageViewHolder) holder;
        viewHolder.tv_item_message_time.setText(TimeUtils.getFormatTime(mData.get(position).getCreate_time() * 1000, "MM-dd"));
        viewHolder.tv_item_message_title.setText(mData.get(position).getTitle());
        viewHolder.tv_item_message_content.setText(Html.fromHtml(mData.get(position).getContent()));
//        if (mData != null) {
//            ResHotPeopleInfo.HotPeopleOrderBean str = mData.get(position);
//            ((HotPeopleViewHolder) holder).init(str);
//        }
//        holder.textView.setText(str);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class MessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tv_item_message_time, tv_item_message_title, tv_item_message_content;
        View view;
        ImageView iv_item_message_icon;


        public MessageViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            tv_item_message_time = itemView.findViewById(R.id.tv_item_message_time);
            tv_item_message_title = itemView.findViewById(R.id.tv_item_message_title);
            tv_item_message_content = itemView.findViewById(R.id.tv_item_message_content);
            iv_item_message_icon = itemView.findViewById(R.id.iv_item_message_icon);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, getAdapterPosition());
            }
        }
    }

}
